module.exports = (sequelize, Sequelize) => {
  const article = sequelize.define('article', {
    id: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
    title: {
      type: Sequelize.STRING(100),
      allowNull: false
    },
    content: {
      type: Sequelize.TEXT,
      allowNull: false
    },
    image: {
      type: Sequelize.STRING(200),
      allowNull: true
    },
    author_id: {
      type: Sequelize.INTEGER,
      allowNull: false,
      references: {
        model: 'User', 
        key: 'id'
      }
    },
    like_count: { 
      type: Sequelize.INTEGER,
      allowNull: false,
      defaultValue: 0
    },
    category: {
      type: Sequelize.STRING(100),
      allowNull: false,
    },
    label_1: {
      type: Sequelize.STRING(100),
      allowNull: true,
    },
    label_2: {
      type: Sequelize.STRING(100),
      allowNull: true,
    },
    label_3: {
      type: Sequelize.STRING(100),
      allowNull: true,
    },
    label_4: {
      type: Sequelize.STRING(100),
      allowNull: true,
    },
    label_5: {
      type: Sequelize.STRING(100),
      allowNull: true,
    },
    created_at: {
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW,     
       get() {
        const date = this.getDataValue('created_at')
        return date ? date.toISOString().slice(0, 10) : null
      }
      
    },
    updated_at: {
      type: Sequelize.DATE,
      allowNull: false,
      defaultValue: Sequelize.NOW,
      get() {
        const date = this.getDataValue('updated_at')
        return date ? date.toISOString().slice(0, 10) : null
      }
    }
  }, {
    timestamps: true,
    underscored: true,
    tableName: 'article'
  })
  article.associate = models => {
    article.belongsTo(models.User, { foreignKey: 'author_id' });
  };

  return article
}